package cn.zhz.service;

import cn.zhz.dao.DeptDao;
import cn.zhz.pojo.Dept;
import org.hibernate.HibernateException;
import org.hibernate.Transaction;

import java.util.List;

public class DeptService {
    DeptDao dao = new DeptDao();

    public void save(Dept dept) {
        Transaction tx = null;

        try {
            tx = dao.currentSession().beginTransaction();

            dao.save(dept);

            tx.commit();
        } catch (HibernateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            if (tx != null) {
                tx.rollback();
            }
        }

    }

    public Dept get(int id) {
        Dept dept = null;

        Transaction tx = null;

        try {
            tx = dao.currentSession().beginTransaction();

            dept = dao.get(id);

            tx.commit();
        } catch (HibernateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            if (tx != null) {
                tx.rollback();
            }
        }

        return dept;
    }

    public Dept load(int id) {
        Dept dept = null;

        Transaction tx = null;

        try {
            tx = dao.currentSession().beginTransaction();

            dept = dao.load(id);
            //System.out.println(dept.getDeptName());
            tx.commit();
        } catch (HibernateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            if (tx != null) {
                tx.rollback();
            }
        }

        return dept;
    }

    public void update(Dept dept) {
        Transaction tx = null;

        try {
            tx = dao.currentSession().beginTransaction();

            Dept deptToUpdate = dao.load(dept.getDeptNo());

            deptToUpdate.setDeptName(dept.getDeptName());
            deptToUpdate.setLocation(dept.getLocation());


            tx.commit();
        } catch (HibernateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            if (tx != null) {
                tx.rollback();
            }
        }
    }

    public void delete(int id) {
        Transaction tx = null;

        try {
            tx = dao.currentSession().beginTransaction();

            Dept dept = dao.load(id);

            dao.delete(dept);

            tx.commit();
        } catch (HibernateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            if (tx != null) {
                tx.rollback();
            }
        }
    }

    public void update2(Dept dept) {
        Transaction tx = null;

        try {
            tx = dao.currentSession().beginTransaction();

            dao.update(dept);

            tx.commit();
        } catch (HibernateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            if (tx != null) {
                tx.rollback();
            }
        }
    }

    public void saveOrUpdate(Dept dept) {
        Transaction tx = null;

        try {
            tx = dao.currentSession().beginTransaction();

            dao.saveOrUpdate(dept);

            tx.commit();
        } catch (HibernateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            if (tx != null) {
                tx.rollback();
            }
        }

    }

    public void merge(Dept dept) {
        Transaction tx = null;

        try {
            tx = dao.currentSession().beginTransaction();

            dao.merge(dept);

            tx.commit();
        } catch (HibernateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            if (tx != null) {
                tx.rollback();
            }
        }

    }

    public List<Dept> findDeptByName(String deptName) {
        Transaction tx = null;
        List<Dept> result = null;

        try {
            tx = dao.currentSession().beginTransaction();
            result = dao.findDeptByName(deptName);
            tx.commit();
        } catch (HibernateException e) {
            e.printStackTrace();
            if (tx != null)
                tx.rollback();
        }
        return result;
    }

    public List<Dept> findDeptByName2(String deptName) {
        List<Dept> list = null;
        Transaction tx = null;

        try {
            tx = dao.currentSession().beginTransaction();

            list = dao.findByDeptName2(deptName);

            tx.commit();
        } catch (HibernateException e) {
            e.printStackTrace();
            if (tx != null) {
                tx.rollback();
            }
        }
        return list;
    }

    public List<String> findAllDeptNames() {
        Transaction tx = null;
        List<String> result = null;
        try {
            tx = dao.currentSession().beginTransaction(); // 开启事务
            result = dao.findAllNames();
            tx.commit(); // 提交事务
        } catch (HibernateException e) {
            e.printStackTrace();
            if (tx != null)
                tx.rollback(); // 回滚事务
        }
        return result;
    }

//    public List<Object[]> findAllDeptList() {
//        Transaction tx = null;
//        List<Object[]> result = null;
//        try {
//            tx = dao.currentSession().beginTransaction(); // 开启事务
//            result = dao.findAllDeptList();
//            tx.commit(); // 提交事务
//        } catch (HibernateException e) {
//            e.printStackTrace();
//            if (tx != null)
//                tx.rollback(); // 回滚事务
//        }
//        return result;
//    }

    public List<Dept> findAllDeptList_2() {
        Transaction tx = null;
        List<Dept> result = null;
        try {
            tx = dao.currentSession().beginTransaction(); // 开启事务
            result = dao.findAllDeptList_2();
            tx.commit(); // 提交事务
        } catch (HibernateException e) {
            e.printStackTrace();
            if (tx != null)
                tx.rollback(); // 回滚事务
        }
        return result;
    }

    public List<Object[]> findByInnerJoin() {
        Transaction tx = null;
        List<Object[]> result = null;
        try {
            tx = dao.currentSession().beginTransaction(); // 开启事务
            result = dao.findByInnerJoin();
            tx.commit(); // 提交事务
        } catch (HibernateException e) {
            e.printStackTrace();
            if (tx != null)
                tx.rollback(); // 回滚事务
        }
        return result;
    }

    public List<Dept> findByInnerJoinFetch() {
        Transaction tx = null;
        List<Dept> result = null;
        try {
            tx = dao.currentSession().beginTransaction(); // 开启事务
            result = dao.findByInnerJoinFetch();
            tx.commit(); // 提交事务
        } catch (HibernateException e) {
            e.printStackTrace();
            if (tx != null)
                tx.rollback(); // 回滚事务
        }
        return result;
    }

    public List<Object[]> findByLeftJoin() {
        Transaction tx = null;
        List<Object[]> result = null;
        try {
            tx = dao.currentSession().beginTransaction(); // 开启事务
            result = dao.findByLeftJoin();
            tx.commit(); // 提交事务
        } catch (HibernateException e) {
            e.printStackTrace();
            if (tx != null)
                tx.rollback(); // 回滚事务
        }
        return result;
    }

    public List<Dept> findByLeftJoinFetch() {
        Transaction tx = null;
        List<Dept> result = null;
        try {
            tx = dao.currentSession().beginTransaction(); // 开启事务
            result = dao.findByleftJoinFetch();
            tx.commit(); // 提交事务
        } catch (HibernateException e) {
            e.printStackTrace();
            if (tx != null)
                tx.rollback(); // 回滚事务
        }
        return result;
    }

    public List<Object[]> findByEqualJoin() {
        Transaction tx = null;
        List<Object[]> result = null;
        try {
            tx = dao.currentSession().beginTransaction(); // 开启事务
            result = dao.findByEqualJoin();
            tx.commit(); // 提交事务
        } catch (HibernateException e) {
            e.printStackTrace();
            if (tx != null)
                tx.rollback(); // 回滚事务
        }
        return result;
    }

    public Long findCount() {
        Transaction tx = null;
        Long count = null;
        try {
            tx = dao.currentSession().beginTransaction(); // 开启事务
            count = dao.findCount();
            tx.commit(); // 提交事务
        } catch (HibernateException e) {
            e.printStackTrace();
            if (tx != null)
                tx.rollback(); // 回滚事务
        }
        return count;
    }

    public List<Dept> findBySal() {
        Transaction tx = null;
        List<Dept> result = null;
        try {
            tx = dao.currentSession().beginTransaction(); // 开启事务
            result = dao.findBySal();
            tx.commit(); // 提交事务
        } catch (HibernateException e) {
            e.printStackTrace();
            if (tx != null)
                tx.rollback(); // 回滚事务
        }
        return result;
    }

    public void addNewDept(Dept dept) {
        Transaction tx = null;
        try {
            tx = dao.currentSession().beginTransaction();
            dao.save(dept);
            tx.commit();
        } catch (HibernateException e) {
            e.printStackTrace();
            if (tx != null)
                tx.rollback();
        }
    }

    public void deleteDept(Dept dept){
        Transaction tx = null;
        try {
            tx=dao.currentSession().beginTransaction();
            dao.delete(dept);
            tx.commit();
        } catch (HibernateException e) {
            e.printStackTrace();
            if(tx!=null)
                tx.rollback();
        }
    }
}
